#!/bin/sh

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@

if [ -d /home/root ]; then
	export HOME=/home/root
	cd
fi

# any debuglevel passed?
if [ -z $ENIGMA_DEBUG_LVL ]; then
	DEBUG_LVL=3
else
	DEBUG_LVL=$ENIGMA_DEBUG_LVL
fi

LIBS=@libdir@/libopen.so.0.0.0

# enigma main loop
while : ; do
	# show bootlogo on enigma2 start
	if [ -x @bindir@/showiframe ]; then
		if [ -f @sysconfdir@/enigma2/backdrop.mvi ]; then
			@bindir@/showiframe @sysconfdir@/enigma2/backdrop.mvi
		elif [ -f @sysconfdir@/enigma2/bootlogo.mvi ]; then
			@bindir@/showiframe @sysconfdir@/enigma2/bootlogo.mvi
		elif [ -f @datadir@/bootlogo.mvi ]; then
			@bindir@/showiframe @datadir@/bootlogo.mvi
		fi
	fi

	# hook to execute scripts always before enigma2 start
	if [ -x @bindir@/enigma2_pre_start.sh ]; then
		@bindir@/enigma2_pre_start.sh
	fi

	# start enigma
	sync
	if [ $DEBUG_LVL -lt 4 ]; then
		LD_PRELOAD=$LIBS ENIGMA_DEBUG_LVL=$DEBUG_LVL @bindir@/enigma2
	else
		# remove old logfiles
		keep=5;
		for file in `ls -t /home/root/enigma.*.debuglog`; do let "keep--"; if [ "$keep" -lt "0" ]; then rm $file; fi; done
		# todays log file
		file="/home/root/enigma.$(date +%Y%m%d).debuglog"
		LD_PRELOAD=$LIBS ENIGMA_DEBUG_LVL=$DEBUG_LVL @bindir@/enigma2 >> $file 2>&1
	fi


	# enigma2 exit codes:
	#
	#  1 - halt
	#  2 - reboot
	#  3 - restart enigma in normal mode
	#  4 - front processor upgrade
	#  5 - install new settings
	#  6 - restart enigma in debug mode
	#  7 - manufacturer reset
	# 42 - offline update
	# 43 - restart for autoinstall
	#
	# >128 signal

	ret=$?
	case $ret in
		1)
			/sbin/halt
			;;
		16)
			echo "rescue" > /proc/stb/fp/boot_mode
			/sbin/reboot
			;;
		2)
			/sbin/reboot
			;;
		3)
			DEBUG_LVL=3
			;;
		4)
			/sbin/rmmod lcd
			/usr/sbin/fpupgrade --upgrade 2>&1 | tee /home/root/fpupgrade.log
			sleep 1;
			/sbin/rmmod fp
			/sbin/modprobe fp
			/sbin/reboot
			;;
		5)
			if ! grep -q config.misc.RestartUI /etc/enigma2/settings; then
				echo "config.misc.RestartUI=true" >>/etc/enigma2/settings
			fi
			;;
		6)
			DEBUG_LVL=4
			;;
		7)
			rm -R /etc/enigma2
			;;
		42)
			df -P | grep -v "tmpfs " | awk '{print $6}' | tail -n +3 > /tmp/upgrade_mountpoints.txt
			while read line; do
				if [  -f $line/var/lib/opkg/status ]; then
				DESTS=$DESTS" --add-dest "$line":"$line
			fi
			done < /tmp/upgrade_mountpoints.txt
			# bind the console (when available)
			[ -f /sys/class/vtconsole/vtcon1/bind ] && echo 1 > /sys/class/vtconsole/vtcon1/bind
			prevupd=0
			currupd=999
			opkg update 2>&1 | tee /home/root/opkgupgrade.log
			# check if we need to upgrade busybox first
			if [ "$(opkg list-upgradable busybox)" != "" ]; then
				opkg update busybox 2>&1 | tee -a /home/root/opkgupgrade.log
			fi
			# update all other packages
			while [ $currupd -gt 0 -a $currupd -ne $prevupd ]; do
				opkg upgrade $DESTS 2>&1 | tee -a /home/root/opkgupgrade.log
				prevupd=$currupd
				currupd=`opkg list-upgradable | wc -l`
				echo "===> $currupd PACKAGE(S) REMAINING" >> /home/root/opkgupgrade.log
			done
			/sbin/reboot
			;;
		43)
			#auto install and autobackup
			[ -f /sys/class/vtconsole/vtcon1/bind ] && echo 1 > /sys/class/vtconsole/vtcon1/bind
			/etc/init.d/settings-restore.sh
			/etc/init.d/avahi-daemon stop
			ifdown eth1
			ip addr flush dev eth1 scope global
			ifdown eth0
			ip addr flush dev eth0 scope global
			/etc/init.d/networking stop
			killall -9 udhcpc
			rm /var/run/udhcpc*
			/etc/init.d/dbus-1 reload
			/etc/init.d/networking start
			/etc/init.d/avahi-daemon start
			# wait until we have a network again
			echo "**** Waiting for network ****"
			for i in {1..30}; do
				gw=`netstat -rn | grep UG | awk '{print $2}'`
				if [ ! -z "$gw" ]; then
					echo "> Online !"
					break
				fi
				sleep 1
			done
			if [ ! -f /etc/.doNotAutoinstall ]; then
				touch /etc/.doAutoinstall
			else
				rm -f /etc/.doNotAutoInstall
			fi
			break
			;;
		*)
			break
			;;
	esac

done
